package org.example.service;

import org.example.spring.BeanPostProcessor;
import org.example.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


//自定义增强器
@Component
public class LeLouchBeanProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean) {
        System.out.println(beanName+"正在执行初始化前.........."+bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean) {
        System.out.println(beanName+"正在执行初始化后--------"+bean);
        System.out.println(bean);
        if ("userService".equals(beanName)){
            Object proxyInstance = Proxy.newProxyInstance(LeLouchBeanProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), (proxy, method, args) -> {
                System.out.println("AOP实现成功");
                return method.invoke(bean, args);
            });
            return proxyInstance;
        }
        return bean;
    }
}
